package com.wolfalpha.lwtool.constant;

import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;

import com.wolfalpha.lwtool.R;
import com.wolfalpha.lwtool.vo.FFCity;

public class StringUtils {

	static String reg = "^[0-9]+";

	// 频率
	public static int freq(Context ox, String frequency) {
		int i = 0;
		for (; i < ox.getResources().getStringArray(R.array.frequency).length; i++) {
			if (ox.getResources().getStringArray(R.array.frequency)[i]
					.equals(frequency)) {
				return (i + 1) * 1000 * 60;
			}
		}
		return 1000;
	}

	// 转换和提取类型
	// fafa
	public static int type2Int(String type) {
		int word = 1;
		if (type.contains("校内"))
			word = 14;
		if (type.contains("传单"))
			word = 11;
		if (type.contains("服务员"))
			word = 15;
		if (type.contains("其他"))
			word = 1;
		if (type.contains("销售"))
			word = 2;
		if (type.contains("安保"))
			word = 3;
		if (type.contains("文员"))
			word = 12;
		if (type.contains("模特"))
			word = 10;
		if (type.contains("家教"))
			word = 9;
		if (type.contains("校内"))
			word = 14;
		if (type.contains("促销"))
			word = 5;
		if (type.contains("校内"))
			word = 14;
		if (type.contains("校内"))
			word = 14;
		if (type.contains("校内"))
			word = 14;
		if (type.contains("校内"))
			word = 14;
		if (type.contains("校内"))
			word = 14;
		return word;
	}

	// 转换和提取工资
	// fafa
	public static int getSalary(String salary) {
		System.out.println(salary);
		String wage = "";

		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(salary);
		while (m.find()) {
			wage = m.group(0);
		}
		if (wage.equals("")) {
			wage = "15";
		}
		return Integer.parseInt(wage);
	}

	// 转换和提取地区
	// fafa
	public static int region2Int(String region) {
		int r = 200;
		if (region.contains("栖霞"))
			r = 300;
		if (region.contains("雨花台"))
			r = 400;
		if (region.contains("六合"))
			r = 1000;
		if (region.contains("浦口"))
			r = 700;
		if (region.contains("秦淮"))
			r = 600;
		if (region.contains("玄武"))
			r = 200;
		if (region.contains("建邺"))
			r = 800;
		if (region.contains("下关"))
			r = 900;
		if (region.contains("江宁"))
			r = 100;
		if (region.contains("开发"))
			r = 1300;
		if (region.contains("溧水"))
			r = 1100;
		if (region.contains("高淳"))
			r = 1200;
		if (region.contains("白下"))
			r = 200;
		if (region.contains("鼓楼"))
			r = 500;
		if (region.contains("栖霞"))
			r = 300;
		return r;
	}

	// 转换和提取结算方式
	// fafa
	public static int wage2Int(String wage) {
		if (wage.contains("周"))
			return 3;
		else if (wage.contains("时"))
			return 1;
		else if (wage.contains("天") || wage.contains("日"))
			return 2;
		else if (wage.contains("月"))
			return 4;
		else if (wage.contains("完工"))
			return 5;
		else
			return 1;
	}

	// 提取时间
	public static int time2Int(String time) {
		String reg1 = "[0-2][0-9](:|：)[0-6][0-9]\\s*(-|~|~)\\s*[0-2][0-9](:|：)[0-6][0-9]";
		Pattern p = Pattern.compile(reg1);
		Matcher m = p.matcher(time);
		String tm = "";
		while (m.find()) {
			tm += m.group(0);
		}

		int time1 = 0;
		tm = tm.replaceAll("-|-", "");
		tm = tm.replaceAll(":|：", "");
		tm = tm.replaceAll("~", "");
		tm = tm.replaceAll("\\s*", "");
		if (tm.length() > 8) {
			tm = tm.substring(0, 7);
		}
		if (tm.equals("")) {
			tm = "9001800";
		}
		try {
			time1 = Integer.parseInt(tm);
		} catch (Exception e) {
			e.printStackTrace();
			time1 = 9001800;
		}
		return time1;
	}

	// 城市ID
	public static int city2Int(String city) {
		for (FFCity fc : Constant.citys) {
			if (fc.getCity().contains(city)) {
				return fc.getId();
			}
		}
		return 0;
	}

	// 工作周期
	public static int peroid2Int(String str) {
		if (str.contains("短期")) {
			return 2;
		} else if (str.contains("长期") || str.contains("不限")) {
			return 1;
		} else if (str.contains("临时")) {
			return 3;
		} else {
			return 1;
		}
	}

	public static String mobile2Long(String mobile) {
		String mob = "";
		String reg = "^1[358]\\d{9}$";
		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(mobile);
		while (m.find()) {
			mob = m.group();
		}
		return mob;
	}

	public static String calendarToString(Calendar calendar) {
		String year = String.valueOf(calendar.get(Calendar.YEAR));
		int mon = calendar.get(Calendar.MONTH) + 1;
		String month = null;
		if (mon < 10) {
			month = "0" + mon;
		} else {
			month = String.valueOf(mon);
		}
		int dayInt = calendar.get(Calendar.DAY_OF_MONTH);
		String day = null;
		if (dayInt < 10) {
			day = "0" + dayInt;
		} else {
			day = String.valueOf(dayInt);
		}
		return year + "-" + month + "-" + day;
	}

	public static Calendar toCalendar(int time) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis((long) time * 1000);
		return calendar;
	}

	public static String parseTime(int time) {
		return calendarToString(toCalendar(time));
	}
}
